<?php

namespace Frieza\Modules\AdServing\Apis;


use App\Library\Common;
use Doraemon\tools\Log;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Doraemon\model\AdServing\Serving;

class ReceiveNotice extends Controller
{
    public function index(Request $request)
    {
        try {
            $params = $request->all();
            Log::debug("无人机通知请求日志-channel-apk", $params);
            $rules = [
                'channel' => 'required',
                'ver' => 'required',
            ];
            $msg = Common::validatorRule($rules, $params);
            if ($msg !== true) {
                throw new \Exception($msg);
            }
            $channel = $params['channel'];
            $version = $params['ver'];
            $channel = explode(',', $channel);
            $data['created_version'] = $version;
            $data['edittime'] = time();
            $where['code'] = $channel;
            $where['terminal'] = Serving::TERMINAL_ANDROID;
            $res = Serving::update($data, $where);
            if ($res->errorCode() !== '00000') {
                throw new \Exception("更新错误");
            }
            $res_data['code'] = 1;
            $res_data['msg'] = '成功';
        } catch (\Exception $e) {
            Log::error("无人机通知错误日志-channel-apk，错误信息：", ['err_msg' => $e->getMessage(), 'params' => $params]);
            $res_data['code'] = 0;
            $res_data['msg'] = $e->getMessage();
        }
        return $res_data;
    }
}